<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use think\Controller;
use think\Db;
use app\api\controller\common;
use app\index\controller\Base;


class Index extends Base
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    protected $task_model = null;
    protected $task_key = null;
    public function _initialize()
    {
        if(is_weixin()) {
                parent::_initialize();

        }else{
            $this->redirect("Web/index");
        }

        $this->task_key = new \app\common\model\shop\task\Key;

        $this->task_model  = new \app\admin\model\shop\Task;
    }

    //任务大厅
    public  function index(){

        $type = input("type")?:1;
        $page = 1;
        $page_size = 10;
        $room = $this->userAttribute["room"];

        $is_task = $this->userAttribute["task_status"];

        $list = [];

        if($is_task==1) {
            $_where = [];
            //$
            $shop_id_arr = [];
            $shop_jingshai_gere_order =  floatval(Db::name("system")->where("key","shop_jingshai_gere_order")->value("value"));//同一家店铺的所有精刷单或隔日单,只能够N月才能看到
            $shop_jingshai_gere_order = time()-$shop_jingshai_gere_order*24*60*60;
            //完成时间
            $r_where["create_time"][] = array("neq",0);
            //状态
            //$r_where["status"] = 13;
            $r_where["task_type"] = array("in",["1","2","3"]);
            $r_where["uid"] = $this->userInfo["uid"];
            //$r_where["finished_time"][] = array("gt",$shop_jingshai_gere_order);//之前是完成时间
            $r_where["create_time"][] = array("gt",$shop_jingshai_gere_order);//之前是接单时间时间
            $shop_id_arr = Db::name("shop_order")->where($r_where)->column("shop_id");//商店id数组

            if ($room == 1) {
                $_where["a.left_task_num_one"] = array(">=", 1);
            } elseif ($room == 2) {
                $_where["a.left_task_num_tow"] = array(">=", 1);
            }


            if($type!=3) {
                $_where["k.real_money"] = array("<=",$this->userAttribute["task_limit_cash"]);
            }

            $_where["k.shop_id"] = array("not in", $shop_id_arr);//商品订单

            $shop_liuliang_order =  floatval(Db::name("system")->where("key","sys_month_liuliang_dan_dianpu_open")->value("value"));//同一家店铺的所有精刷单或隔日单,只能够N月才能看到

            //
            if($shop_liuliang_order==0 && $type==3){//$shop_liuliang_order：0 显示  1：关闭
                unset($_where["k.shop_id"]);
            }
            //删除任务不显示
            //$_where["delete_time"] = array("neq","null");

            $_join[] = ["sd_shop_task k", "k.task_id = a.task_id", "left"];
            $list = Db::name("shop_task_key")
                ->alias("a")
                ->join($_join)
                ->where($_where)
                ->where("k.task_type", $type)
                ->where("k.task_status", 2)
                ->field("a.key_word,k.goods_sno,a.take_key_id")
                ->page($page, $page_size)
                ->group("a.take_key_id")
                ->order("a.create_time DESC")
                ->select();
        }
        $this->assign("type",$type);
        $this->assign("type_r",1);
        $this->assign("list",$list);

        return $this->fetch('/index');
    }

    //任务列表
    public function  taskList(){
        //halt(input('page'));
        $task_type = input('type');//task_type:1；精刷单，2：隔日单，3：流量单
        $page      = input('page')?:1;//页数
        $page_size = input('page_size')?:10;//页码数
        $_where = [];
        $room = $this->userAttribute["room"];
        $data = [
            "count"=>0,
            "type"=>$task_type,
            "curr"=>1,
            "list"=>[]
        ];
        $is_task = $this->userAttribute["task_status"];
        if($is_task==1) {
            $shop_id_arr = [];
            $shop_jingshai_gere_order =  floatval(Db::name("system")->where("key","shop_jingshai_gere_order")->value("value"));//同一家店铺的所有精刷单或隔日单,只能够N月才能看到
            $shop_jingshai_gere_order = time()-$shop_jingshai_gere_order*24*60*60;
            //完成时间
            $r_where["create_time"][] = array("neq",0);
            $r_where["uid"] = $this->userInfo["uid"];
            //状态
            //$r_where["status"] = 13;
            $r_where["task_type"] = array("in",["1","2","3"]);
            //$r_where["finished_time"][] = array("gt",$shop_jingshai_gere_order);//之前是完成时间
            $r_where["create_time"][] = array("gt",$shop_jingshai_gere_order);//之前是接单时间时间


            $shop_id_arr = Db::name("shop_order")->where($r_where)->column("shop_id");//商店id数组


            if ($room == 1) {
                $_where["a.left_task_num_one"] = array(">=", 1);
            } elseif ($room == 2) {
                $_where["a.left_task_num_tow"] = array(">=", 1);
            }
            if($task_type!=3) {
                $_where["k.real_money"] = array("<=",$this->userAttribute["task_limit_cash"]);
            }


            $_where["k.shop_id"] = array("not in", $shop_id_arr);//商品订单

            $shop_liuliang_order =  floatval(Db::name("system")->where("key","sys_month_liuliang_dan_dianpu_open")->value("value"));//同一家店铺的所有精刷单或隔日单,只能够N月才能看到
            //
            if($shop_liuliang_order==0 && $task_type==3){//$shop_liuliang_order：0 显示  1：关闭
                unset($_where["k.shop_id"]);
            }
            //删除任务不显示
            //$_where["delete_time"] = array("neq","null");


            $_join[] = ["sd_shop_task k", "k.task_id = a.task_id", "left"];
            $data["count"] = ceil((Db::name("shop_task_key")->alias("a")->join($_join)->where("k.task_type", $task_type)->where("a.left_task_num_one", ">=", 1)->count()) / $page_size);
            $data["type"] = $task_type;
            $data["curr"] = $page;
            $data["list"] = Db::name("shop_task_key")
                ->alias("a")
                ->join($_join)
                ->where($_where)
                //s->where("a.left_task_num_one",">=",1)
                ->where("k.task_type", $task_type)
                ->where("k.task_status", 2)
                ->field("a.key_word,k.goods_sno,a.take_key_id")
                ->page($page, $page_size)
                ->group("a.take_key_id")
                ->order("a.create_time DESC")
                ->select();


        }
        renderJson("任务列表",1,$data);

    }


    //注意事项
    public function attention($task_key_id=NULL){

        $this->assign("user_info",$this->userInfo);
        $this->assign("user_attribute",$this->userAttribute);
        $this->assign("user_real",$this->userReal);
        $this->assign("task_key_id",$task_key_id);
        return $this->fetch("shop/attention");

    }


    //邀请注册接口
    /**
     * 邀请注册的接口
     *
     */
    public function  invitationAccept(){

        $uid = $this->uid;
        $role = Db::name("user")->where("uid",$uid)->value("role");
        if($role=="grouper"){
            $data["link"] = config("app.sys_domain").url("index/index")."?lock=".config("app.sys_lock")."&parent_id=".$this->uid;//分享链接
        }elseif($role=="user"){
            $data["link"] = config("app.sys_domain").url("index/index")."?parent_id=".$this->uid;//分享链接
        }
        $data["title"]="邀请注册";//标签
        $data["desc"] = "欢迎来到征服世界";//描述
        $data["imgUrl"] = config("app.sys_domain")."/static/img/zfz.png";//分享图标
        renderJson("分享成功",1,$data);


    }




}
